﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Vouchers;
using Lists;
using BasicForms;
using GeneralLibs;

namespace BanLe
{
    public partial class FrmDanhSachHoaDonBanLe : BasicForms.FrmDanhSachChungTu
    {
        public FrmDanhSachHoaDonBanLe()
        {
            InitializeComponent();
            _DieuKienTimKiem.LoaiChungTu = DanhMucLoaiChungTu.Instance.GetByMa("HoaDonBanLe");
            //ThoiDiemKhoaSo thoiDiemKhoaSoGanNhat = DanhMucThoiDiemKhoaSo.Instance.GetThoiDiemKhoaSoGanNhat(DateTime.Now);
            //if (thoiDiemKhoaSoGanNhat != null)
            //    _DieuKienTimKiem.TuNgay = new NullableDate(thoiDiemKhoaSoGanNhat.ThoiDiem);
            //else
            //    _DieuKienTimKiem.TuNgay = new NullableDate(new DateTime(CommonLib.GetSystemDateTime().Date.Year, 1, 1, 0, 0, 1));

            ////Nếu là SupAdmin thì chỉ hiện tháng gần nhất cho nhẹ
            //if (CustomSecurity.User.CurUser.UserName == "SupAdmin")
            //{
            //    _DieuKienTimKiem.TuNgay = new NullableDate(new DateTime(CommonLib.GetSystemDateTime().Date.Year, CommonLib.GetSystemDateTime().Date.Month, 1, 0, 0, 1));
            //}

            DateTime fivedayago = DateTime.Now.AddDays(-5);
            _DieuKienTimKiem.TuNgay = new NullableDate(new DateTime(fivedayago.Year, fivedayago.Month, fivedayago.Day, 0, 0, 1));
            _DieuKienTimKiem.DenNgay = new NullableDate(new DateTime(CommonLib.GetSystemDateTime().Date.Year, 12, 31, 23, 59, 59));
            _DieuKienTimKiem.ASC = false;
        }

        void frm_RequestAddNew()
        {
            try
            {
                FrmEditHoaDonBanLe1366x768 frm = new FrmEditHoaDonBanLe1366x768(_DanhSachChungTu.CreateNew(_DieuKienTimKiem.LoaiChungTu));
                frm.RequestAddNew += new FrmEditHoaDonBanLe1366x768.OnRequestAddNew(frm_RequestAddNew);
                //frm.RequestAddNew += new FrmEditChungTu.OnRequestAddNew(frm_RequestAddNew);
                frm.Show(); BringFormToFront(frm);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        public override void CreateNew()
        {
            try
            {
                FrmEditHoaDonBanLe1366x768 frm = new FrmEditHoaDonBanLe1366x768(_DanhSachChungTu.CreateNew(_DieuKienTimKiem.LoaiChungTu));
                //frm.RequestAddNew += new FrmEditChungTu.OnRequestAddNew(frm_RequestAddNew);
                frm.RequestAddNew += new FrmEditHoaDonBanLe1366x768.OnRequestAddNew(frm_RequestAddNew);
                frm.Show(); BringFormToFront(frm);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }
        protected override void Edit()
        {
            try
            {
                ArrayList arDeletedChungTu = new ArrayList();
                foreach (int index in gridViewDsChungTu.GetSelectedRows())
                {
                    arDeletedChungTu.Add(_DanhSachChungTu.GetByDataRow(gridViewDsChungTu.GetDataRow(index)));
                }
                gridViewDsChungTu.BeginUpdate();
                foreach (ChungTu chungTu in arDeletedChungTu)
                {
                    int desksHeights = Screen.PrimaryScreen.Bounds.Height;
                    int desksWidths = Screen.PrimaryScreen.Bounds.Width;

                    try
                    {
                        BanLe.FrmEditHoaDonBanLe1366x768 frm = new BanLe.FrmEditHoaDonBanLe1366x768(chungTu);
                        frm.RequestAddNew += new FrmEditHoaDonBanLe1366x768.OnRequestAddNew(frm_RequestAddNew);
                        frm.Show(); BringFormToFront(frm);
                    }
                    catch (Exception ex)
                    {
                    }
                    BasicForms.FrmProgressBarDev.FinishShowProgress();
                }
                gridViewDsChungTu.EndUpdate();
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void FrmDanhSachHoaDonBanLe_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            barButtonItemNew.Enabled = false;
            barButtonItemOpen.Enabled = false;
        }
    }
}
